﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using OpenMms.Common.Auth;
using OpenMms.Domain.Interface.User;
using OpenMms.Domain.ReponseModels;
using OpenMms.Domain.RequestModels;
using X.PagedList;

namespace OpenMms.MVC.Areas.User.Controllers
{
    /// <summary>
    /// 会员升级
    /// </summary>
    [Area("User")]
    public class UserUpgradeController :BaseController
    {
        #region 注入
        private IUserUpgradeService _service;
        public UserUpgradeController(IAuth auth, IUserUpgradeService service) : base(auth) => this._service = service;
        #endregion

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public async Task<IActionResult> Index(int? pageIndex)
        {
            var session = _auth.GetCurrentUser();

            var userinfo = await _service.GetInforMations(session.Account);

            var memberLevels = await _service.QueryMemberLevel(userinfo.UserLevel);
            //初始化等级下拉框
            var selectItemList = new List<SelectListItem>() {
                new SelectListItem(){Value="0",Text="请选择会员级别",Selected=true}
            };

            var selectList = new SelectList(memberLevels, "LevelID", "LevelName", "LevelID");

            selectItemList.AddRange(selectList);

            ViewBag.database = selectItemList;
            //获取升级历史
            var list = await _service.UpRecord(userinfo.UserCode, pageIndex);

            return View(list);
        }
        #endregion

        #region 分布视图
        /// <summary>
        /// 分部视图
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public async Task<IActionResult> List(int? pageIndex)
        {
            var userCode = _auth.GetCurrentUser().Account;
            var list = await _service.UpRecord(userCode, pageIndex);
            return PartialView(list);
        }
        #endregion

        #region 操作
        /// <summary>
        /// 会员升级操作
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<JsonResult> Upgrade(UserUpGradeReq model)
        {
            model.AfterLevel = Convert.ToInt16(Request.Form["database-dropdownlist"]);
            if (model.AfterLevel == 0)
            {
                return Json (new Response()
                {
                    Code = 204,
                    Message = "请选择级别"
                });
            }
            var sessionmodel = _auth.GetCurrentUser();
            model.UserCode = sessionmodel.Account;
            var result = await _service.Do(model);
            return Json(result);
        }
        #endregion

        #region 计算差额
        /// <summary>
        /// 计算差额
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<string> Difference(UserUpGradeReq model)
        {
            var amount = await _service.CalcAmount(model);
            return amount.ToString("f2");
        }
        #endregion

    }
}